package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.Group;
import controller.TableModelGroup;

@SuppressWarnings("serial")
public class PanelGroupList extends JPanel {

	private static PanelGroupList single;

	JTable GroupList = new JTable(new TableModelGroup());
	JScrollPane JSP = new JScrollPane(GroupList);

	private JButton addCB = new JButton("Nieuwe klas");
	private JButton Details = new JButton("Klas details");

	private PanelGroupList() {

		setBounds(0, 0, 860, 720);
		setLayout(null);
		
		GroupList.setRowSelectionInterval(0, 0);

		JSP.setBounds(280, 100, 140, 500);

		addCB.setBounds(1, 100, 140, 40);
		Details.setBounds(1, 150, 140, 40);

		addCB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				PanelHolder mf = PanelHolder.getInstance();
				mf.remove(PanelGroupList.getInstance());
				mf.add(PanelAddGroup.newInstance());
				mf.repaint();
				mf.validate();
			}
		});

		Details.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Group g = ((TableModelGroup) GroupList.getModel()).getSelectedRowObject(GroupList.getSelectedRow());
				
				PanelGroupDetails.getInstance().updateGroupDetail(g);

				PanelGroupDetails.getInstance().getJLKlas();
				 
				PanelGroupDetails.getInstance().getJLName().setText(g.getGroup_name());
				 
				PanelGroupDetails.getInstance().getJTName().setText(g.getGroup_name());

				 PanelHolder ph = PanelHolder.getInstance();
				 ph.remove(PanelGroupList.getInstance());
				 ph.add(PanelGroupDetails.getInstance());
				 ph.repaint();
				 ph.validate();
				
			}
		});

		add(JSP);
		add(addCB);
		add(Details);

	}

	public static PanelGroupList getInstance() {
		if (single == null) {
			single = new PanelGroupList();
		}
		return single;
	}

	public void selfDestroy() {
		single = null;
	}

	public JTable getGroupList() {
		return GroupList;
	}
}
